Reflection in C# is a powerful feature that allows you to
inspect, analyze, and even modify the structure of your code (like classes, methods, properties, etc.)
at runtime. It’s part of the System.Reflection namespace.
What is Reflection?
Reflection allows your program to:
- Inspect assemblies
- Examine types (classes, interfaces, structs)
- Access metadata (attributes, method info)
- Dynamically create instances
- Invoke methods and access properties/fields
It's widely used in:
- Frameworks like Entity Framework, ASP.NET MVC
- Serialization/Deserialization
- Unit Testing
- Plugins and dynamic module loading
Namespaces Required
using System;
using System.Reflection;
Basic Example
using System;
using System.Reflection;
class Person
{
public string Name { get; set; }
public void SayHello()
{
Console.WriteLine("Hello!");
}
}
class Program
{
static void Main()
{
Type type = typeof(Person); // Get type info
Console.WriteLine("Class Name: " + type.Name);
Console.WriteLine("\nProperties:");
foreach (PropertyInfo prop in type.GetProperties())
{
Console.WriteLine(prop.Name);
}
Console.WriteLine("\nMethods:");
foreach (MethodInfo method in type.GetMethods())
{
Console.WriteLine(method.Name);
}
}
}
Common Reflection Classes
| Class | Description |
|---|---|
Type |
Represents type declarations (class, struct, etc.) |
PropertyInfo |
Represents a property |
MethodInfo |
Represents a method |
FieldInfo |
Represents a field |
ConstructorInfo |
Represents a constructor |
Assembly |
Represents an entire .NET assembly (DLL/EXE) |
Getting Type Info
Type t1 = typeof(Person); // Compile time
Type t2 = obj.GetType(); // Runtime
Type t3 = Type.GetType("Namespace.Person");
Invoking a Method via Reflection
Person p = new Person();
MethodInfo method = typeof(Person).GetMethod("SayHello");
method.Invoke(p, null); // Calls SayHello on p
Accessing Fields & Properties
PropertyInfo prop = typeof(Person).GetProperty("Name");
prop.SetValue(p, "Alice");
Console.WriteLine(prop.GetValue(p)); // Output: Alice
Creating Objects Dynamically
Type type = typeof(Person);
object obj = Activator.CreateInstance(type); // Creates new Person()
MethodInfo method = type.GetMethod("SayHello");
method.Invoke(obj, null);
Reading Attributes
[Obsolete("Use NewClass instead")]
class OldClass { }
Type type = typeof(OldClass);
object[] attrs = type.GetCustomAttributes(false);
foreach (object attr in attrs)
{
Console.WriteLine(attr);
}
Reflection Limitations
- Performance: Slower than direct access.
- Security: Can bypass encapsulation.
- Complexity: Harder to maintain/debug.
Summary
| Concept | Description |
|---|---|
| What is it? | A way to inspect metadata at runtime |
| Key Namespace | System.Reflection |
| Use Cases | Serialization, DI, Frameworks, Testing |
| Main Class | Type |
| Dynamic Method Call | MethodInfo.Invoke() |
| Create Object | Activator.CreateInstance() |
Reflection gives C# incredible flexibility and power, especially for frameworks and tools. But use it wisely — only when necessary — to avoid complexity and performance issues.
Read More -
Leave Comment